const app = getApp(),url = app.globalData.url;
var lineHeight = 0;
var endWords = "";
var isNum;
Page({
  data: {
    index:0,
    arr:[],
    num:0,//已选个数
    xuanzhongarr:[
      // {id:1,name:'IT技术'},{id:3,name:'技术支持'},{id:2,name:'销售'}
    ],
    // 承载器
    xzarr:[],
    xzobj:{},
    searchArr:[],//搜索的数据
  },
  yes_but(){
    var that = this
    var xuanzhongarr = that.data.xuanzhongarr
    console.log(xuanzhongarr)
    var s = xuanzhongarr.join(",");
    // var hangye = that.data.hangye
    wx.setStorageSync('hangye', s)
    that.setData({searchArr:[]})
    wx.navigateBack({
      delta: 1,
    })
  },
  reset(){
    var that = this
    that.setData({searchArr:[]})
  },
  onLoad: function (options) {
    var that = this
    var index = that.data.index
    wx.request({
      url:url+'occupation/getOccupationType',
      header:{
      'context-type': 'application/json',
      },
      method: "get",
      data:{},
      success:function(res){
        var ret = res.data
        console.log('行业',res)
        if(ret.code == 200){
          that.setData({
            arrlist:ret.data,
            fid:ret.data[index].id,
          })
        }else{
          wx.showToast({
            title: ret.msg,
            icon:'none',
          })
        }
        var cid = ret.data[index].occOrder
        console.log('cid',cid)
        that.ziliebiao(cid)
      }
    })
    // var arr = that.data.arr
    // var index = that.data.index
    // that.setData({
    //   arrlist:arr,
    //   fid:arr[index].id,
    //   hangye:arr[index].fname,//行业 村缓存
    //   arrchilder:arr[index].childer//二级数组
    // })
  },
  // 二级分类
  ziliebiao(id){
    var that = this
    console.log(id)
    wx.request({
      url:url+'occupation/getOccupationItem',
      header:{
      'context-type': 'application/json',
      },
      method: "get",
      data:{
        occ_order:id
      },
      success:function(res){
        var ret = res.data
        console.log('zi业',res)
        if(ret.code == 200){
          that.setData({arrchilder:ret.data,})
        }else{
          wx.showToast({
            title: ret.msg,
            icon:'none',
          })
        }
      }
    })
  },
  // 点一级分类
  btn(e){
    var that = this
    // var id = e.currentTarget.dataset.id
    var index = e.currentTarget.dataset.index
    // var arr = that.data.arr
    var arr = that.data.arrlist
    // console.log(index)
    // console.log(arr)
    that.setData({
      arrlist:arr,
      fid:arr[index].id,
      // arrchilder:arr[index].contentSet
    })
    that.ziliebiao(arr[index].occOrder)
  },
  xuanzesearch(e){
    var that = this
    var xzarr = this.data.xzarr//承载器[]
    var hangye = e.currentTarget.dataset.hangye;
    var _index = xzarr.indexOf(hangye)
    if(_index==-1){
      xzarr.push(hangye)
      // xzarr.push(item.flname)
    }else{
      xzarr.splice(_index,1)
    }
    that.setData({
      xuanzhongarr:xzarr,//已选中的数组
      num:xzarr.length,//已选长度
    })
  },
  // 选择 选中
  xuanzeid: function (e) {
    console.log(e)
    var num = this.data.num
    if(num<3){
      var hangye = e.currentTarget.dataset.hangye;
      var index = e.currentTarget.dataset.index;
      var item = this.data.arrchilder[index];
      var xzarr = this.data.xzarr//承载器[]
      var _index = xzarr.indexOf(item.name)
      // var _index = xzarr.indexOf(item.flname)
      if(_index==-1){
        xzarr.push(item.name)
        // xzarr.push(item.flname)
      }else{
        xzarr.splice(_index,1)
      }
      item.isSelected = !item.isSelected;
      this.setData({
        hangye:hangye,//行业 村缓存
        arrchilder: this.data.arrchilder,
        xuanzhongarr:xzarr,//已选中的数组
        num:xzarr.length,//已选长度
      });
    }else{
      return false
    }
// 备份 对象设置 对象设置 对象设置 对象设置 
      //创建对象  放入数组
      // var xzobj = this.data.xzobj//承载器[]
      // xzobj ={
      //   id:item.id,
      //   name:item.flname
      // }
      // xzarr.push(xzobj)
      // console.log('下边',xzarr)
      // item.isSelected = !item.isSelected;
      // this.setData({
      //   arrchilder: this.data.arrchilder,
      //   xuanzhongarr:xzarr,//已选中的数组
      //   num:xzarr.length,//已选长度
      // });
    
  },
  // 移除
  yichu(e){
    var that = this
    var name = e.currentTarget.dataset.name
    var arrchilder = that.data.arrchilder//这个是 二级分类的数组   判断ture false
    var index = e.currentTarget.dataset.index
    var xuanzhongarr = that.data.xuanzhongarr
    //判断选中状态
    // for(var k=0;k<arrchilder.length;k++){
      // console.log(arrchilder[k].flname)
      // if(arrchilder[k].name == name){//arrchilder[k].flname
      //   arrchilder[k].isSelected = false;
        //移除点中的
        for(var i=0; i<xuanzhongarr.length; i++){
          if(i === index){
            xuanzhongarr.splice(i,1);
          }
        }
        // break;
      // }else{
      //   // wx.showToast({
      //   //   title: '此列表无此数据',
      //   //   icon:'none'
      //   // })
      // }
    // }

    that.setData({
      arrchilder:arrchilder,//判断选中状态
      xuanzhongarr:xuanzhongarr,
      num:xuanzhongarr.length
    })
  }, 
  
  
  onReady: function () {
    var that = this
  },
  onShow: function () {
    // 生命周期函数--监听页面显示
 
  },

  searchBut(e){
    var that = this
    console.log(e.detail.value)
    var index = that.data.index
    wx.request({
      url:url+'occupation/searchOccupationItem',
      method: "GET",
      data:{
        occupation_name :e.detail.value
      },
      success:function(res){
        var ret = res.data
        console.log('回车',res)
        if(ret.code == 200){
          if(ret.data ==''){
            wx.showToast({
              title: "未搜索到数据",
              icon: 'none',
            })
          }else{
            that.setData({
              searchArr:ret.data
            })
          }
        }else{
          wx.showToast({
            title: ret.msg,
            icon:'none',
          })
        }
      }
    })
    
  },
  
})